import { defineStore } from 'pinia'
import { ref } from 'vue'
import {
  getToken,
  removeToken,
  removeTokenTime,
  setToken,
  setTokenTime
} from '@/utils/token'
import { TABS_VALUE, TAB_VALUE, DARK_KEY } from '@/layout/utils/constant'
import { LANG } from '@/utils/constant'

export default defineStore('app', () => {
  const app = ref<string>('app')
  const lang = ref<string>(localStorage.getItem('lang') || 'zh')
  const theme = ref<boolean>(localStorage.getItem(DARK_KEY) === 'dark')
  const isCollapse = ref<boolean>(false)
  const isLogin = ref<boolean>(getToken() ? true : false)
  const token = ref<string | null>(getToken())
  const userInfo = ref()
  // 登录
  const login = async (loginMessage: LoginMessage) => {}
  // 获取登录的用户信息
  const getUserInfo = async () => {}
  // 登出
  const logout = async () => {
    clear()
  }
  // 清除数据
  const clear = () => {
    removeToken()
    removeTokenTime()
    localStorage.removeItem(TAB_VALUE)
    localStorage.removeItem(TABS_VALUE)
    localStorage.removeItem(DARK_KEY)
    localStorage.removeItem(LANG)
    sessionStorage.removeItem('isHello')
    window.location.reload()
  }

  return {
    theme,
    app,
    lang,
    isCollapse,
    isLogin,
    token,
    login,
    logout,
    userInfo,
    getUserInfo,
    clear
  }
})
